package com.baidu.dao.impl;

import com.baidu.common.bean.Role;
import com.baidu.common.bean.User;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.JdbcConstant;
import com.baidu.dao.UserDao;
import com.baidu.dao.abstractclass.BaseAbstract;
import com.baidu.dao.util.JdbcUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/19/20:02
 */
public class UserDaoImpl extends BaseAbstract<User> implements UserDao {

    public UserDaoImpl() throws WebException {
    }

    @Override
    public User get(Integer id) throws WebException {
        PreparedStatement queryStatement = null;
        ResultSet userResultSet = null;
        try {
            queryStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.USER_QUERYBYID));
            queryStatement.setInt(1,id);
            userResultSet = queryStatement.executeQuery();
            return JdbcUtil.getUser(userResultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(queryStatement,userResultSet);
        }
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int add(User user) throws WebException {
        PreparedStatement userAddStatement = null;
        try {
            int index = 0;
            userAddStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.USER_INSERT));
            userAddStatement.setInt(++index,0);
            userAddStatement.setString(++index,user.getUserName());
            userAddStatement.setString(++index,user.getAccount());
            userAddStatement.setString(++index,user.getPassword());
            userAddStatement.setBigDecimal(++index,user.getBalance());
            StringBuilder roleIds = new StringBuilder();
            List<Role> roles = user.getRoles();
            for (Role role : roles) {
                roleIds.append(role.getId()).append("-");
            }
            userAddStatement.setString(++index,user.getRoles().size()==0?null: roleIds.toString());
            int isUpdateSuccess = userAddStatement.executeUpdate();
            connection.commit();
            return isUpdateSuccess;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
                throw new WebException(e.getMessage());
            }
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(userAddStatement);
        }
    }

    @Override
    public User getByLoginSql(String userName, String password) throws WebException {
        ResultSet resultSet = null;
        try {
            resultSet = JdbcUtil.executeQuery("select id,userName,`account`,`password`,`balance`,roleId from tbl_user where account='" + userName + "' and `password`='" + password+"'");
            return JdbcUtil.getUser(resultSet);
        } catch (WebException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeResultSet(resultSet);
        }
    }

    @Override
    public User getByLoginNsql(String userName, String password) throws WebException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.USER_QUERY_LOGIN));
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,password);
            resultSet = preparedStatement.executeQuery();
            return JdbcUtil.getUser(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(preparedStatement,resultSet);
        }
    }

    @Override
    public User getUserByAccount(String account) throws WebException {
        PreparedStatement getUserStatement = null;
        try {
            getUserStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.USER_QUERY_BY_ACCOUNT));
            getUserStatement.setString(1,account);
            return JdbcUtil.getUser(getUserStatement.executeQuery());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(getUserStatement);
        }
    }

    @Override
    public List<User> queryGoodsTypeList() throws WebException {
        PreparedStatement queryListStatement = null;
        ResultSet userResultSet = null;
        try {
            queryListStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.USER_QUERY_ALL));
            userResultSet = queryListStatement.executeQuery();
            return JdbcUtil.getUsers(userResultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(queryListStatement,userResultSet);
        }
    }
}
